1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| /*** * 弹框提示 */ public class AlertDialog { private Context context; private Dialog dialog; private LinearLayout lLayout_bg; private TextView txt_title; private TextView txt_msg; private Button btn_neg; private Button btn_pos; private ImageView img_line; private Display display; private boolean showTitle = false; private boolean showMsg = false; private boolean showPosBtn = false; private boolean showNegBtn = false;
public AlertDialog(Context context) { this.context = context; WindowManager windowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); display = windowManager.getDefaultDisplay(); }
public AlertDialog builder() { View view = LayoutInflater.from(context).inflate( R.layout.view_alert_dialog, null);
lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg); txt_title = (TextView) view.findViewById(R.id.txt_title); txt_msg = (TextView) view.findViewById(R.id.txt_msg); btn_neg = (Button) view.findViewById(R.id.btn_neg); btn_pos = (Button) view.findViewById(R.id.btn_pos); img_line = (ImageView) view.findViewById(R.id.img_line); setGone(); dialog = new Dialog(context, R.style.AlertDialogStyle); dialog.setContentView(view); lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display .getWidth() * 0.85), LayoutParams.WRAP_CONTENT));
return this; }
/** * 恢复初始 * @return */ public AlertDialog setGone() { if (lLayout_bg != null) { txt_title.setVisibility(View.GONE); txt_msg.setVisibility(View.GONE); btn_neg.setVisibility(View.GONE); btn_pos.setVisibility(View.GONE); img_line.setVisibility(View.GONE);
} showTitle = false; showMsg = false; showPosBtn = false; showNegBtn = false; return this; }
/** * 设置title * @param title * @return */ public AlertDialog setTitle(String title) { showTitle = true; if (TextUtils.isEmpty(title)) { txt_title.setText("提示"); } else { txt_title.setText(title); } return this; }
/** * 设置Message * @param msg * @return */ public AlertDialog setMsg(String msg) { showMsg = true; if (TextUtils.isEmpty(msg)) { txt_msg.setText(""); } else { txt_msg.setText(msg); } return this; }
/** * 设置点击外部是否消失 * @param cancel * @return */ public AlertDialog setCancelable(boolean cancel) { dialog.setCancelable(cancel); return this; }
/** * 右侧按钮 * * @param text * @param listener * @return */ public AlertDialog setPositiveButton(String text, final OnClickListener listener) { return setPositiveButton(text, -1, listener); }
public AlertDialog setPositiveButton(String text, int color, final OnClickListener listener) { showPosBtn = true; if ("".equals(text)) { btn_pos.setText(""); } else { btn_pos.setText(text); } if (color == -1) { color = R.color.action_sheet_blue; } btn_pos.setTextColor(ContextCompat.getColor(context, color)); btn_pos.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (listener != null) listener.onClick(v); dismiss(); } }); return this; }
/** * 左侧按钮 * * @param text * @param listener * @return */
public AlertDialog setNegativeButton(String text, final OnClickListener listener) {
return setNegativeButton(text, -1, listener); }
public AlertDialog setNegativeButton(String text, int color, final OnClickListener listener) { showNegBtn = true; if ("".equals(text)) { btn_neg.setText(""); } else { btn_neg.setText(text); } if (color == -1) { color = R.color.action_sheet_blue; } btn_neg.setTextColor(ContextCompat.getColor(context, color));
btn_neg.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (listener != null) listener.onClick(v); dismiss(); } }); return this; }
/** * 设置显示 */ private void setLayout() { if (!showTitle && !showMsg) { txt_title.setText(""); txt_title.setVisibility(View.VISIBLE); }
if (showTitle) { txt_title.setVisibility(View.VISIBLE); }
if (showMsg) { txt_msg.setVisibility(View.VISIBLE); }
if (!showPosBtn && !showNegBtn) { btn_pos.setText(""); btn_pos.setVisibility(View.VISIBLE); btn_pos.setBackgroundResource(R.drawable.alert_dialog_selector); btn_pos.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); }
if (showPosBtn && showNegBtn) { btn_pos.setVisibility(View.VISIBLE); btn_pos.setBackgroundResource(R.drawable.alert_dialog_right_selector); btn_neg.setVisibility(View.VISIBLE); btn_neg.setBackgroundResource(R.drawable.alert_dialog_left_selector); img_line.setVisibility(View.VISIBLE); }
if (showPosBtn && !showNegBtn) { btn_pos.setVisibility(View.VISIBLE); btn_pos.setBackgroundResource(R.drawable.alert_dialog_selector); }
if (!showPosBtn && showNegBtn) { btn_neg.setVisibility(View.VISIBLE); btn_neg.setBackgroundResource(R.drawable.alert_dialog_selector); } }
public void show() { setLayout(); dialog.show(); }
public boolean isShowing() { if (dialog != null) { if (dialog.isShowing()) return true; else return false; } return false; }
public void dismiss() { if (dialog!=null){ dialog.dismiss(); }
} }
|